
package com.taku.adprofadapter;


import static com.taku.adprofadapter.AdprofInitNetwork.getAppId;
import static com.taku.adprofadapter.AdprofInitNetwork.getCodeId;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.adprof.sdk.api.AdError;
import com.adprof.sdk.api.AdRequest;
import com.adprof.sdk.api.InterstitialAd;
import com.adprof.sdk.api.InterstitialAdListener;
import com.anythink.core.api.ATAdConst;
import com.anythink.core.api.ATBiddingListener;
import com.anythink.core.api.ATBiddingResult;
import com.anythink.core.api.ATInitMediation;
import com.anythink.core.api.MediationInitCallback;
import com.anythink.interstitial.unitgroup.api.CustomInterstitialAdapter;

import java.util.Map;


public class AdprofInterstitialAdapter extends CustomInterstitialAdapter {
    public static String TAG = AdprofInitNetwork.TAG;

    InterstitialAd mInterAd;

    String mAppId;
    String slot_Id;

    boolean c2s_Bidding;

    @Override
    public boolean startBiddingRequest(final Context context, Map<String, Object> serverExtra, Map<String, Object> localExtra, ATBiddingListener biddingListener) {
        //   serverExtra = {ad_format=3, ad_type=-1, gdpr_consent=true, app_coppa_switch=false, gm_currency=RMB, forbid_gm_refresh=true, sys_sp=2, app_ccpa_switch=false, anythink_mediation_wf_id=b68f83c16c1fbe_746836_0_be2a26f90037707f10558a3886c4e7a1, slot_id=xxx, ad_s_reqf_mode=1, app_id=xxx, app_secret=xxxx, unit_id=xxx}

        Log.d(TAG, "\n inter startBiddingRequest   serverExtra = " + serverExtra + "   localExtra = " + localExtra + "   biddingListener = " + biddingListener);

        c2s_Bidding = true;

        loadCustomNetworkAd(context, serverExtra, localExtra);

        return true;
    }

    @Override
    public void loadCustomNetworkAd(final Context context, final Map<String, Object> serverExtra, final Map<String, Object> localExtra) {

        mAppId = getAppId(serverExtra);
        slot_Id = getCodeId(serverExtra);
        Log.d(TAG, "loadCustomNetworkAd: mAppId = " + mAppId + "  mADUnitId = " + slot_Id + "   isC2SBidding =  " + c2s_Bidding);

        if (TextUtils.isEmpty(mAppId)) {
            notifyATLoadFail("", "Adprof app_id is empty.");
            return;
        }

        AdprofInitNetwork.getInstance().initSDK(context, serverExtra, new MediationInitCallback() {
            @Override
            public void onSuccess() {
                loadInterstitial(serverExtra, localExtra);
            }

            @Override
            public void onFail(String errorMsg) {
                notifyATLoadFail("", errorMsg);
            }
        });
    }

    private void loadInterstitial(Map<String, Object> serverExtra, Map<String, Object> localExtra) {
        AdRequest adRequest = new AdRequest.Builder()
                .setCodeId(slot_Id)
                .build();

        mInterAd = new InterstitialAd(adRequest, new InterstitialAdListener() {

            @Override
            public void onInterstitialAdLoadError(AdError adError) {
                notifyATLoadFail(String.valueOf(adError.getErrorCode()), adError.getMessage());
            }

            @Override
            public void onInterstitialAdLoadSuccess() {
                if (c2s_Bidding) {

                    if (mBiddingListener != null && mInterAd != null) {
                        int ecpm = mInterAd.getBidPrice();

                        AdprofBiddingNotify biddingNotice = new AdprofBiddingNotify(mInterAd);

                        mBiddingListener.onC2SBiddingResultWithCache(ATBiddingResult.success(ecpm, System.currentTimeMillis() + "", biddingNotice, ATAdConst.CURRENCY.RMB_CENT), null);
                    }

                } else if (mLoadListener != null) {
                    mLoadListener.onAdDataLoaded();
                }
            }

            @Override
            public void onInterstitialAdLoadCached() {
                if (mLoadListener != null) {
                    mLoadListener.onAdCacheLoaded();
                }
            }

            @Override
            public void onInterstitialAdShow() {
                if (mImpressListener != null) {
                    mImpressListener.onInterstitialAdShow();
                }

                if (mImpressListener != null) {
                    mImpressListener.onInterstitialAdVideoStart();
                }
            }

            @Override
            public void onInterstitialAdPlayEnd() {
                if (mImpressListener != null) {
                    mImpressListener.onInterstitialAdVideoEnd();
                }
            }

            @Override
            public void onInterstitialAdClick() {
                if (mImpressListener != null) {
                    mImpressListener.onInterstitialAdClicked();
                }
            }

            @Override
            public void onInterstitialAdClosed() {
                if (mImpressListener != null) {
                    mImpressListener.onInterstitialAdClose();
                }
            }

            @Override
            public void onInterstitialAdShowError(AdError adError) {
                if (mImpressListener != null) {
                    mImpressListener.onInterstitialAdVideoError(adError.getErrorCode() + "", adError.getMessage());
                }
            }

        });

        mInterAd.loadAd();
    }

    @Override
    public boolean isAdReady() {
        if (mInterAd != null) {
            return mInterAd.isReady();
        }
        return false;
    }

    @Override
    public void show(Activity activity) {

        if (mInterAd == null) {
            return;
        }

        if (isAlive(activity)) {
            mInterAd.showAd(activity);
        }
    }

    private static boolean isAlive(Activity activity) {
        return activity != null && !activity.isFinishing() && !activity.isDestroyed();
    }

    @Override
    public String getNetworkName() {
        return AdprofInitNetwork.getInstance().getNetworkName();
    }

    @Override
    public String getNetworkPlacementId() {
        return slot_Id;
    }

    @Override
    public String getNetworkSDKVersion() {
        return AdprofInitNetwork.getInstance().getNetworkVersion();
    }

    @Override
    public ATInitMediation getMediationInitManager() {
        return AdprofInitNetwork.getInstance();
    }

    @Override
    public void destory() {
        try {
            if (mInterAd != null) {
                mInterAd.destroyAd();
                mInterAd = null;
            }
        } catch (Exception e) {
            Log.d(TAG, "destory: " + Log.getStackTraceString(e));
        }
    }

}
